package com.dswz.controller.admin;

import com.dswz.po.Admin;
import com.dswz.service.AdminService;
import com.dswz.service.SessionService;
import com.dswz.utils.AdminResult;
import com.dswz.utils.EncryptUtil;
import com.dswz.utils.RequestUtils;
import com.dswz.utils.Utils;
import com.dswz.utils.image.ValidateCode;
import com.google.gson.Gson;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/admin" })
public class LogInAdminController {
	private Logger logger = LogManager.getLogger(LogInAdminController.class);
	@Autowired
	private AdminService adminService;
	@Autowired
	private SessionService sessionService;

	@RequestMapping({ "/toLogin" })
	@ResponseBody
	public Object toLogin(HttpServletRequest request, HttpServletResponse response, Model model) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_SESSION");
			this.logger.info("页面来源:" + request.getHeader("referer") + "客户端:" + request.getHeader("user-agent")
					+ " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: " + username);
			this.logger.error("用户未登录!");
			result.setResult(1);
			result.setMessage("用户未登录");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping({ "/adminLogin" })
	@ResponseBody
	public Object adminLogin(String username, String password, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			this.logger.info("username:" + username + "password:" + password + "页面来源:" + request.getHeader("referer")
					+ "客户端:" + request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request));
			if (StringUtils.isEmpty(username)) {
				this.logger.error("请输入用户名!");
				result.setMessage("请输入用户名!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(password)) {
				this.logger.error("请输入密码!");
				result.setMessage("请输入密码!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(request.getParameter("code"))) {
				this.logger.error("请输入验证码!");
				result.setMessage("请输入验证码!");
				result.setResult(3);
				return result;
			}

			String validateCode = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_VALIDATECODE_SESSION");
			if (StringUtils.isEmpty(validateCode)) {
				this.logger.error("请获取验证码!");
				result.setMessage("请获取验证码!");
				result.setResult(3);
				return result;
			}
			if (!validateCode.equalsIgnoreCase(request.getParameter("code"))) {
				this.logger.error("验证码错误!");
				result.setMessage("验证码错误!");
				result.setResult(3);
				return result;
			}

			Admin admin = this.adminService.getAdmin(username, Byte.valueOf((byte) 0));
			if (admin == null) {
				this.logger.error("用户名不正确!");
				result.setMessage("用户名不正确!");
				result.setResult(3);
				return result;
			}
			String pass=EncryptUtil.encrypt(EncryptUtil.getDefaultKey(), password);
			if (pass.equals(admin.getAdminPassword())) {
				this.sessionService.setAttribute(RequestUtils.getCSESSIONID(request, response), "ADMIN_SESSION",
						admin.getAdminName());
			} else {
				this.logger.error("密码不正确!");
				result.setMessage("密码不正确!");
				result.setResult(3);
				return result;
			}
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping({ "/validateCode" })
	public void validateCode(Model model, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		ValidateCode vc = new ValidateCode(120, 40, 4, 0);
		try {
			this.logger.info("页面来源" + request.getHeader("referer") + "客户端:" + request.getHeader("user-agent")
					+ " 客户端ip: " + Utils.getUserRealAddress(request));
			String code = vc.getCode();

			this.sessionService.setAttribute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_VALIDATECODE_SESSION", code);
			vc.write(response.getOutputStream());
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + code + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
		}
	}
}
